package idv.takeshi.software.productline.bookstore.domain.model.order;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A model as a PickingList.
 * @author takeshi
 *
 */
@Entity
@Table(name = "PICKING_LIST")
public class PickingList {
	@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
	private Long id;
	@Column(nullable = false)
	private int sequence;
	@Column(nullable = false, length = 50)
	@Enumerated(EnumType.STRING)
	private PickingListStatus status;
	@Column(nullable = false, name = "CREATE_TIME")
	private Date createTime;
	@Column(nullable = false, name = "UPDATE_TIME") 
	private Date updateTime;
	 
	@OneToMany(cascade = CascadeType.ALL)
	@JoinColumn(name = "PICKING_LIST_ID", nullable = false)
	@OrderBy("sequence ASC")
	private Set<PickingListItem> pickingListItems = new LinkedHashSet<PickingListItem>();


	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}


	/**
	 * @return the sequence
	 */
	public int getSequence() {
		return sequence;
	}


	/**
	 * @param sequence the sequence to set
	 */
	public void setSequence(int sequence) {
		this.sequence = sequence;
	}


	/**
	 * @return the status
	 */
	public PickingListStatus getStatus() {
		return status;
	}


	/**
	 * @param status the status to set
	 */
	public void setStatus(PickingListStatus status) {
		this.status = status;
	}


	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}


	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}


	/**
	 * @return the updateTime
	 */
	public Date getUpdateTime() {
		return updateTime;
	}


	/**
	 * @param updateTime the updateTime to set
	 */
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	
	/**
	 * Add a pickingListItem.
	 * @param orderItem
	 * @return
	 */
	public PickingListItem addPickingListItem(OrderItem orderItem){
		Validate.notNull(orderItem, "orderItem shall always not be null");
		PickingListItem pickingListItem = new PickingListItem();
		
		pickingListItem.setSequence(this.getNewSequence());
		pickingListItem.setOrderItem(orderItem);
		this.pickingListItems.add(pickingListItem);
		
		return pickingListItem;
	}
	
	/**
	 * Get the unmodifiable pickingListItems
	 * @return the pickingListItems
	 */
	public Set<PickingListItem> getPickingListItems() {
		return Collections.unmodifiableSet(pickingListItems);
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (!(obj instanceof PickingList)) {
		     return false;
		   }
		   PickingList rhs = (PickingList) obj;
		   return new EqualsBuilder()
			   .append(this.id, rhs.id)
			   .append(this.sequence, rhs.sequence)
			   .append(this.status, rhs.status)
			   .append(this.createTime, rhs.createTime)
			   .append(this.updateTime, rhs.updateTime)
			   .append(this.pickingListItems, rhs.pickingListItems)
		       .isEquals();
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(29, 31).
	       append(this.id).
	       append(this.sequence).
	       append(this.status).
	       append(this.createTime).
	       append(this.updateTime).
	       append(this.pickingListItems).
	       toHashCode();
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
	}


	/**
	 * Generate new sequence.
	 * @return
	 */
	private int getNewSequence(){
		int sequence = 1;
		if(pickingListItems.size() > 0){
			sequence = pickingListItems.size() + 1;
		}
		return sequence;
	}
	
	
	 
}
 
